package com.ia.bikerecife;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.example.bikerecife.R;
import com.ia.bikerecife.maps.Coordenate;

public class SettingsActivity extends Activity {

    public static String     IP                 = "192.168.0.103";
    public static int        PORT               = 7777;
    public static boolean    mUseUnicapLocation = false;

    public static Coordenate mUnicapLocation    = new Coordenate(-8.056011, -34.887821);

    private EditText         mEditTextIPP1;
    private EditText         mEditTextIPP2;
    private EditText         mEditTextIPP3;
    private EditText         mEditTextIPP4;
    private EditText         mEditTextPort;

    private Switch           mSwitchUnicapLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setings);
        String[] values = IP.split("\\.");

        mEditTextIPP1 = ((EditText) SettingsActivity.this.findViewById(R.id.editText1));
        mEditTextIPP1.setText(values[0]);
        mEditTextIPP1.setOnClickListener(onClickClear);

        mEditTextIPP2 = ((EditText) SettingsActivity.this.findViewById(R.id.editText2));
        mEditTextIPP2.setText(values[1]);
        mEditTextIPP2.setOnClickListener(onClickClear);

        mEditTextIPP3 = ((EditText) SettingsActivity.this.findViewById(R.id.editText3));
        mEditTextIPP3.setText(values[2]);
        mEditTextIPP3.setOnClickListener(onClickClear);

        mEditTextIPP4 = ((EditText) SettingsActivity.this.findViewById(R.id.editText4));
        mEditTextIPP4.setText(values[3]);
        mEditTextIPP4.setOnClickListener(onClickClear);

        mEditTextPort = ((EditText) SettingsActivity.this.findViewById(R.id.editTextPort));
        mEditTextPort.setText(String.valueOf(SettingsActivity.PORT));
        mEditTextPort.setOnClickListener(onClickClear);

        ((Button) findViewById(R.id.buttonOK)).setOnClickListener(clickListener);

        mSwitchUnicapLocation = (Switch) findViewById(R.id.switchUseUnicapLocation);
        mSwitchUnicapLocation.setChecked(SettingsActivity.mUseUnicapLocation);

    }

    private OnClickListener clickListener = new OnClickListener() {

                                              @Override
                                              public void onClick(View v) {
                                                  SettingsActivity.IP = mEditTextIPP1.getText().toString() + "."
                                                          + mEditTextIPP2.getText().toString() + "."
                                                          + mEditTextIPP3.getText().toString() + "."
                                                          + mEditTextIPP4.getText().toString();

                                                  SettingsActivity.PORT = Integer.parseInt(mEditTextPort.getText()
                                                          .toString());

                                                  Log.v("leac", "IP: " + SettingsActivity.IP + " Port: "
                                                          + SettingsActivity.PORT);

                                                  mUseUnicapLocation = mSwitchUnicapLocation.isChecked();

                                                  SettingsActivity.this.finish();

                                              }
                                          };

    private OnClickListener onClickClear  = new OnClickListener() {

                                              @Override
                                              public void onClick(View v) {
                                                  ((EditText) v).setText("");
                                              }
                                          };
}
